package com.jindu.producer.finance.controller;


import com.jindu.kernel.page.AlphaPage;
import com.jindu.kernel.result.JsonResult;
import com.jindu.kernel.utils.AlphaBeanUtils;
import com.jindu.kernel.utils.DateUtils;
import com.jindu.kernel.utils.IdGenerator;
import com.jindu.producer.finance.jpa.ReplyJpa;
import com.jindu.producer.finance.model.FExperEntity;
import com.jindu.producer.finance.model.FReplyEntity;
import com.jindu.producer.finance.model.FReportEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jarvis
 * @date 2018/10/18
 */
@Api(description = "用户回复管理", tags = "ReplyController", basePath = "/reply")
@RestController
@RequestMapping(value = "/reply",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ReplyController {

    @Autowired
    ReplyJpa replyJpa;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据产品ID，查询用户的评论列表信息", notes = "返回用户的评论列表信息")
    @RequestMapping(value = "getByProductId",method = RequestMethod.GET)
    public JsonResult<AlphaPage<FReplyEntity>> getByProductId(@RequestParam(value = "pageNo",defaultValue = "0") Integer pageNo,
                                                              @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                                              @RequestParam String productId){

        Specification querySpecifi = (Specification<FReplyEntity>) (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if(productId != null){
                predicates.add(criteriaBuilder.equal(root.get("productId"),productId));
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Page<FReplyEntity> entities=replyJpa.findAll(querySpecifi,new PageRequest(pageNo, pageSize,sort));
        return new AlphaPage<>(entities).toJsonResult();
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "保存评测报告的信息", notes = "保存评测报告的信息")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public JsonResult<FReplyEntity> save(@RequestBody FReplyEntity replyEntity){

        FReplyEntity fReplyEntity=replyJpa.findById(replyEntity.getId());

        if(fReplyEntity == null){
            replyEntity.setId(IdGenerator.uuid());
            replyEntity.setCreateTime(new Timestamp(DateUtils.getCurrentTime()));
            replyEntity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
            return JsonResult.getSuccessResult(replyJpa.saveAndFlush(replyEntity));
        }else {
            replyEntity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
            BeanUtils.copyProperties(replyEntity,fReplyEntity, AlphaBeanUtils.getNullPropertyNames(replyEntity));
            return JsonResult.getSuccessResult(replyJpa.saveAndFlush(fReplyEntity));
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "删除回复评论的信息", notes = "根据ID,删除数据")
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public JsonResult<Integer> delete(@RequestBody String id){
        return JsonResult.getSuccessResult(replyJpa.deleteById(id));
    }


}
